home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / rwvector.lha / RWVector2.1 / src / xvecio.cc < prev    next >
C/C++ Source or Header  |  1989-08-18  |  3KB  |  126 lines

  1. /*
  2.  *    Definitions for <T>Vec I/O
  3.  *
  4.  *    Copyright (C) 1988, 1989.
  5.  *
  6.  *    Dr. Thomas Keffer
  7.  *    Rogue Wave Associates
  8.  *    P.O. Box 85341
  9.  *    Seattle WA 98145-1341
  10.  *
  11.  *    Permission to use, copy, modify, and distribute this
  12.  *    software and its documentation for any purpose and
  13.  *    without fee is hereby granted, provided that the
  14.  *    above copyright notice appear in all copies and that
  15.  *    both that copyright notice and this permission notice
  16.  *    appear in supporting documentation.
  17.  *    
  18.  *    This software is provided "as is" without any
  19.  *    expressed or implied warranty.
  20.  *
  21.  *
  22.  *    @(#)xvecio.cc    2.1    8/18/89
  23.  */
  24.  
  25. #define NO_VECTOR_MATHFUN
  26. #include "rw/<T>Vec.h"
  27. #include <stream.h>
  28. #include <ctype.h>
  29.  
  30. #define DEFAULT_RESIZE 128
  31. #define DEFAULT_PERLINE 5
  32.  
  33. static int default_resize = DEFAULT_RESIZE;
  34.  
  35. void
  36. <T>Vec::boundsErr(int i)
  37. {
  38.   char msg[120];
  39.   sprintf(msg, "Index (%d) out of range [0->%d].", i, int(length()-1));
  40.   RWnote("<T>Vec::operator[]", msg);
  41.   RWerror(DEFAULT);
  42. }
  43.  
  44. void
  45. <T>Vec::lengthErr(int i)
  46. {
  47.   char msg[120];
  48.   sprintf(msg, "Lengths do not match: %d versus %d", length(), i);
  49.   RWnote("<T>Vec::", msg);
  50.   RWerror(DEFAULT);
  51. }
  52.  
  53. void
  54. <T>Vec::sliceErr(unsigned vecmax, int pos, unsigned n, int s)
  55. {
  56.   char msg[120];
  57.   sprintf(msg,
  58.       "slice(%d, %u, %d) cannot be constructed from vector %u elements long.",
  59.       pos, n, s, vecmax);
  60.   RWnote("<T>Vec::slice()", msg);
  61.   RWerror(DEFAULT);
  62. }
  63.  
  64. void
  65. <T>Vec::emptyErr(const char* fname)
  66. {
  67.   cerr << "** <T>Vec::\n";
  68.   RWnote(fname, "Vector empty.");
  69.   RWerror(DEFAULT);
  70. }
  71.  
  72. void
  73. <T>Vec::scanFrom(istream& s)
  74. {
  75.   <T> item;
  76.   register int nextspace = 0;
  77.   char c = 0;        // The first character read from the stream
  78.  
  79.   s >> c;
  80.   while(isspace(c)) s >> c;
  81.   if (c != '[') {
  82.     // Scan input stream, resizing as necessary.
  83.     // Keep scanning till we can't scan no more
  84.     s.putback(c);
  85.     while(s >> item){
  86.       if(nextspace >= length())resize(length()+default_resize);
  87.       (*this)(nextspace++) = item;
  88.     }
  89.   }
  90.   else {  // Scan input stream, stop scanning at the matching ']' character
  91.     s >> c;
  92.     while( s && (c != ']') ) {
  93.       s.putback(c);
  94.       if (s >> item) {
  95.         if(nextspace >= length())resize(length()+default_resize);
  96.         (*this)(nextspace++) = item;
  97.       }
  98.       s >> c;
  99.     }
  100.   }
  101.   // Trim to fit
  102.   if(nextspace != length())resize(nextspace);
  103. }
  104.  
  105. void
  106. <T>Vec::printOn(ostream& s)
  107. {
  108.   if(numberPerLine<=0)numberPerLine = DEFAULT_PERLINE;
  109.   for(register int n = 0; n < length(); n++){
  110.     if(!(n%numberPerLine) && n) s << "\n";
  111.     s << (*this)(n) << " ";
  112.   }
  113. }
  114.  
  115. ostream&
  116. operator<<(ostream& s, const <T>Vec& v)
  117. {
  118.   v.printOn(s); return s;
  119. }
  120. istream&
  121. operator>>(istream& s, <T>Vec& v)
  122. {
  123.   v.scanFrom(s); return s;
  124. }
  125.  
  126.